package test.doubleindex;

public class Solution2 {

    public ListNode deleteDuplicates(ListNode head) {
        //在此处写入代码

        if (head == null || head.next == null) {
            return head;
        }
        ListNode prev = null;
        ListNode current = head;
        ListNode delete = null;
        while (current != null && current.next != null) {
            if (delete != null && head.val == delete.val) {
                head = null;
            }
            if (delete != null && delete.val == current.val) {
                current = current.next;
            } else if (current.val == current.next.val) {
                delete = current;
                current = current.next.next;

                if (prev != null) {
                    prev.next = current;
                }

            } else {
                if (head == null) {
                    head = current;
                }
                prev = current;
                current = current.next;
            }


        }
        if (head == null && delete != null && delete.val != current.val) {
            head = current;
        }

        return  head;

    }

    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }
}
